<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证0-255</title>
    <style>
        .box{
            display: flex;
            width: 310px;
            position: relative;
        }
        span{
            position: absolute;
            right: 0;
            top: 0;
        }
        .conform{
            color: #58bc58;
        }
        .unConform{
            color: red;
        }
    </style>
</head>
<body>
    <div class="box">
        <input type="text"><button id='btn'>验证</button>
        <span class="unConform"></span>
    </div>

    <script>
        /* 
            思路：
                0-255->(0-9)+(10-99)+(100-199)+(200-249)+(250-255) 

            错误范例：
                var reg = /^[0-255]$/  // 只会得到0、1、2、5
        */
        
        // 正则验证【自身+课堂】
        var reg = /^([0-9]|([1-9]\d)|(1\d\d)|(2[0-4]\d)|(25[0-5]))$/;       // /^([0-9]|(\d){2}|(1\d{2})|(2[0-4]\d)|(25[0-5]))$/

        // 获取元素
        var btn = document.querySelector('#btn');
        var span = document.querySelector('span');

        // 点击验证
        btn.onclick = function(){
            var number = document.querySelector('input').value;
            var isConform = reg.test(number);
            // console.log(isConform);
            if(isConform){
                span.innerText = '通过验证';
                span.className = 'conform';
            }else{
                span.innerText = '不符合要求';
                span.className = 'unConform';
            }
        }
    </script>
</body>
</html>